package com.menu.system.service.impl;

import com.menu.common.utils.DateUtils;
import com.menu.system.domain.ReceptionUser;
import com.menu.system.mapper.MenuUserRoleMapper;
import com.menu.system.mapper.receptionUserMapper;
import com.menu.system.service.IreceptionUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-06
 */
@Service
public class receptionUserServiceImpl implements IreceptionUserService {
    @Autowired
    private receptionUserMapper receptionUserMapper;
    @Autowired
    private MenuUserRoleMapper menuUserRoleMapper;

    /**
     * 查询用户信息
     *
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public ReceptionUser selectreceptionUserByUserId(Long userId) {
        return receptionUserMapper.selectreceptionUserByUserId(userId);
    }

    /**
     * 查询用户信息列表
     *
     * @param receptionUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<ReceptionUser> selectreceptionUserList(ReceptionUser receptionUser) {
        return receptionUserMapper.selectreceptionUserList(receptionUser);
    }

    /**
     * 新增用户信息
     *
     * @param receptionUser 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertreceptionUser(ReceptionUser receptionUser) {
        receptionUser.setCreateTime(DateUtils.getNowDate());
        int i = receptionUserMapper.insertreceptionUser(receptionUser);
        List<ReceptionUser> receptionUsers=new ArrayList<>();

        for (Long roleId : receptionUser.getRoleIds()) {
            receptionUserMapper.batchUserRole(receptionUser.getUserId(),roleId);
        }





        return i;
    }

    /**
     * 修改用户信息
     *
     * @param receptionUser 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updatereceptionUser(ReceptionUser receptionUser) {
        receptionUser.setUpdateTime(DateUtils.getNowDate());
        int i = receptionUserMapper.updatereceptionUser(receptionUser);
        receptionUserMapper.deleterbatchUserRoleIds(receptionUser.getUserId());
        for (Long roleId : receptionUser.getRoleIds()) {
            receptionUserMapper.batchUserRole(receptionUser.getUserId(),roleId);
        }



        return i;
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deletereceptionUserByUserIds(Long[] userIds) {
        int i = receptionUserMapper.deletereceptionUserByUserIds(userIds);
        for (Long userId : userIds) {
            receptionUserMapper.deleterbatchUserRoleIds(userId);
        }


        return  i ;
    }

    /**
     * 删除用户信息信息
     *
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deletereceptionUserByUserId(Long userId) {
        return receptionUserMapper.deletereceptionUserByUserId(userId);
    }
}
